﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ykglxt
{
    public partial class Form_YPSCS : Form
    {
        public Form_YPSCS()
        {
            InitializeComponent();
        }

        SqlConnection con;
        SqlDataAdapter sda;
        DataSet ds;
        BindingSource bs;
        SqlCommandBuilder builder;

        private void Form_YPSCS_Load(object sender, EventArgs e)
        {
            con = new SqlConnection(PublicUnit.ConnetionString);
            sda = new SqlDataAdapter("SELECT * FROM Dic_SCS", con);
            builder = new SqlCommandBuilder(sda);
            ds = new DataSet();
            bs = new BindingSource();
            dgvYPSCS.AutoGenerateColumns = true;
            try
            {
                sda.Fill(ds);
                dgvYPSCS.DataSource = ds.Tables[0];
                dgvYPSCS.Columns[0].HeaderText = "编码";
                dgvYPSCS.Columns[1].HeaderText = "助记符";
                dgvYPSCS.Columns[2].HeaderText = "名称";
                dgvYPSCS.Columns[3].HeaderText = "注册编码";
                dgvYPSCS.Columns[4].HeaderText = "地址";
                dgvYPSCS.Columns[5].HeaderText = "联系人";
                dgvYPSCS.Columns[6].HeaderText = "联系电话";
                dgvYPSCS.Columns[7].HeaderText = "传真号码";
                dgvYPSCS.Columns[8].HeaderText = "邮政编码";
                dgvYPSCS.Columns[9].HeaderText = "生产许可证";
                dgvYPSCS.Columns[10].HeaderText = "电子邮件";
                dgvYPSCS.Columns[11].HeaderText = "网址";
                bs.DataSource = ds.Tables[0];
                bNavigator.BindingSource = bs;
                dgvYPSCS.Rows[0].Selected = true;
            }
            catch (SqlException ex)
            {
                if (ex.Number == 18456)
                    MessageBox.Show("网络故障，请稍后再试！", "警告" + ex.Number, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

         private void bindingNavigatorMovePreviousItem_Click(object sender, EventArgs e)
        {
            int rowindx = (dgvYPSCS.SelectedRows[0].Index - 1 + dgvYPSCS.RowCount) % dgvYPSCS.RowCount;
            dgvYPSCS.ClearSelection();
            dgvYPSCS.Rows[rowindx].Selected = true;
            dgvYPSCS.FirstDisplayedScrollingRowIndex = dgvYPSCS.SelectedRows[0].Index;
        }

        private void bindingNavigatorMoveNextItem_Click(object sender, EventArgs e)
        {
            int rowIndex = (dgvYPSCS.SelectedRows[0].Index + 1) % dgvYPSCS.RowCount;
            dgvYPSCS.ClearSelection();
            dgvYPSCS.Rows[rowIndex].Selected = true;
            dgvYPSCS.FirstDisplayedScrollingRowIndex = rowIndex;
            if(rowIndex < dgvYPSCS.FirstDisplayedScrollingRowIndex)
            {
                dgvYPSCS.FirstDisplayedScrollingRowIndex = rowIndex;
            }
            else if(!dgvYPSCS.Rows[rowIndex].Displayed)
            {
                dgvYPSCS.FirstDisplayedScrollingRowIndex++;
            }

        }

        private void bindingNavigatorMoveFirstItem_Click(object sender, EventArgs e)
        {
            dgvYPSCS.ClearSelection();
            dgvYPSCS.Rows[0].Selected = true;
            dgvYPSCS.FirstDisplayedScrollingRowIndex = 0;
        }

        private void bindingNavigatorMoveLastItem_Click(object sender, EventArgs e)
        {
            dgvYPSCS.ClearSelection();
            dgvYPSCS.Rows[dgvYPSCS.RowCount - 2].Selected = true;
            dgvYPSCS.FirstDisplayedScrollingRowIndex = dgvYPSCS.RowCount - 1;

        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {
            dgvYPSCS.ClearSelection();
            dgvYPSCS.Rows[dgvYPSCS.NewRowIndex].Selected = true;
            dgvYPSCS.FirstDisplayedScrollingRowIndex = dgvYPSCS.NewRowIndex;
        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认删除该记录？", "请选择", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                dgvYPSCS.Rows.RemoveAt(this.dgvYPSCS.CurrentRow.Index);
            }
        }

        private void tsbSave_Click(object sender, EventArgs e)
        {
            dgvYPSCS.CurrentCell = null;
            try
            {
                MessageBox.Show("保存成功，已保存" + sda.Update(ds).ToString() + "条修改记录！","提示",MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch(SqlException ex)
            {
                if(ex.Number == 2627)
                    MessageBox.Show("保存失败，编码必须是唯一的，请查看是否存在重复的编码！","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                else if(ex.Number == 515)
                    MessageBox.Show("保存失败，编码不能为空，请查看是否存在记录缺填编码！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                else
                    MessageBox.Show("保存失败，网络故障，请稍后再试！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
    }
}
